package com.superman.supermarket.client.view.addrchoose;

import java.util.List;
import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.supermarket.client.composite.addrlist.AddrChooseModule;
import com.superman.supermarket.client.gin.ShopGinjector;
import com.superman.supermarket.client.widget.button.TextButton;
import com.superman.supermarket.client.widget.header.PageHeader;

/**
 * 地址选择ui实现类
 * 
 * @author superman
 * @version 2018年8月2日下午4:54:52
 */
public class AddrChooseViewImpl extends FlowPanel implements AddrChooseView {

	private FlowPanel listpanel;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月10日 下午10:38:23
	 */
	public AddrChooseViewImpl() {
		PageHeader pageHeader = new PageHeader();
		pageHeader.setText("选择收货地址");
		TextButton textButton = new TextButton();
		textButton.setText("新建");
		pageHeader.addToWrapper(textButton);
		textButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				XGinjector.INSTANCE.getMyPlaceController().push(ShopGinjector.INSTANCE.getMyPlaceFactory().getAddressNewPlace());
			}
		});

		listpanel = new FlowPanel();
		listpanel.addStyleName("flex-v flex-vc");

		this.add(pageHeader);
		this.add(listpanel);
	}

	@Override
	public void init() {

	}

	@Override
	public void setAddrList(List<Map<String, Object>> list, String selectedAddrId) {
		listpanel.clear();
		for (Map<String, Object> map : list) {
			AddrChooseModule addrChooseModule = new AddrChooseModule();
			addrChooseModule.setData(map);
			boolean select = selectedAddrId.equals(String.valueOf(map.get("ADDR_ID")));
			addrChooseModule.select(select);
			if (select)
				listpanel.insert(addrChooseModule, 0);
			else
				listpanel.add(addrChooseModule);
		}
	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		// TODO Auto-generated method stub

	}
}
